﻿//-----------------------------------------------------------------------
// <copyright file="IMessageManager.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Message
{
    using System;

    /// <summary>
    /// Interface for the message manager which is responsible for distributing
    /// messages to consumers which have registered for a message type.
    /// 
    /// Implementations of the IMessageManager interface must satisfy the following
    /// constraints:
    ///     * Implementation must be thread safe for message sends and message
    ///       registration.
    ///       
    ///     * If three messages, A, B and C, are sent A, then B and then C, they must
    ///       be delivered in the same order they were sent.
    ///
    ///     * All messages must be delivered during the call to DispatchMessages().
    ///     
    ///     * Message registering / sending must support distribution of messages to 
    ///       clients who register for a base class in a class hierarchy as well as for
    ///       an interface class.
    ///       
    ///       For example, let's saw we have Message A as a base class and Message B and C,
    ///       which derive from it. If a client registers for message to type Message A,
    ///       when Message B and C are sent they should receive those messages.
    ///       
    ///       In the case of interfaces, if Message A implements the IMessageA interface then
    ///       clients should be able to register for the IMessageA interface and receive
    ///       all Message A messages.
    ///       
    ///     * Any message sent, regardless of channel, must always be delivered to clients
    ///       listening on the global channel.
    /// </summary>
    public interface IMessageManager
    {
        /// <summary>
        /// Called to deliver all messages. Messages should be delivered immediately when this is called. This
        /// function should only allow a single call to it at a time.
        /// </summary>
        void DispatchMessages();

        /// <summary>
        /// Send a message on the passed channel.
        /// 
        /// If the channel is not the global channel, the message must be delivered to consumers listening to
        /// the channel as well as those listening to the global channel.
        /// 
        /// Before the message is stored for delivery, it must have Validate() called on it to enture it is a
        /// valid message.
        /// </summary>
        /// <param name="channel">Channel to send the message on. If null, the global channel will be used.</param>
        /// <param name="msg">Message to send.</param>
        void SendMessage(MessageChannel channel, MessageBase msg);

        /// <summary>
        /// Register a handler for the passed type of message.
        /// </summary>
        /// <typeparam name="T">Type of message to handle</typeparam>
        /// <param name="channel">Channel to listen to the message for on. If null, the global channel will be used.</param>
        /// <param name="handler">Message handler to call when the message is received.</param>
        /// <param name="priority">Message handler priority</param>
        /// <param name="filter">Filter to use for message filtering.</param>
        void RegisterForMessage<T>(
            MessageChannel channel,
            MessageHandler<T> handler,
            int priority = MessageHandlerPriority.Default,
            IMessageFilter filter = null) where T : MessageBase;

        /// <summary>
        /// Unregister a handler for the passed type of message.
        /// </summary>
        /// <typeparam name="T">Type of message to handle</typeparam>
        /// <param name="channel">Channel the handler was registered on. If null, the global channel will be used.</param>
        /// <param name="handler">Message handler to remove.</param>
        void UnregisterForMessage<T>(MessageChannel channel, MessageHandler<T> handler) where T : MessageBase;
    }
}
